<template>
  <div class="constentmain h100">
    <list-table-new v-bind="tableSetting" ref="klTable"></list-table-new>
    <dialogPublic
      :title="title"
      :show.sync="show"
      v-if="show"
      :btnText="['保存继续', '取消']"
      @doSubmit="doSubmit"
      @doClose="doClose"
    >
      <addDialog ref="addDialog"></addDialog>
    </dialogPublic>
  </div>
</template>
<script>
import {
  querySchedulPlanList,
  addOrUpdateSchedulPlan,
} from "@/api/operationManagement/runningSchedule.js";
import addDialog from "./addDialog.vue";
export default {
  components: {
    addDialog,
  },
  data() {
    return {
      title: "新增排班方案",
      show: false,
      disabled: false,
      tableSetting: {
        selectionShow: false,
        pageConfig: {
          pageCode: "schedulePlan",
          userId: this.$store.getters.userInfo.id,
        },
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "organization",
              label: "值班单位",
              model: "deptName",
              idsStr: "deptId",
              placeholder: "请选择",
            },
            {
              type: "input",
              label: "方案名称",
              model: "name",
              placeholder: "请输入",
            },
            {
              type: "location",
              label: "值班地点",
              model: "locationName",
              idsStr: "locationId",
              placeholder: "请输入",
            },
            {
              type: "select",
              label: "值班人员类型",
              model: "staffTypes",
              multiple:true,
              option: this.$store.getters.dirtData("operation_staff_type"),
            },
            {
              type: "datetimerange",
              label: "开始时间",
              model: "startDates",
              placeholder: "请选择",
              dateFormat: "yyyy-MM-dd HH:mm:ss",
            },
            {
              type: "datetimerange",
              label: "结束时间",
              model: "endDates",
              placeholder: "请选择",
            },
            {
              type: "userOnly",
              label: "创建人",
              model: "createByName",
              idsStr: "createBy",
            },
            {
              type: "daterange",
              label: "创建时间",
              model: "createTime",
            },
          ],
        },
        //过滤器的值
        filterFormData: {},
        //过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        actionList: [
          {
            name: "新增",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("add"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.add();
            },
          },
        ],
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {
            this.goDetailPage(row.id, "scheduleDetails", "edit");
          },
        },
        column: [
          {
            label: "值班单位",
            prop: "deptName",
            sortable: "custom",
          },
          {
            label: "方案名称",
            prop: "name",
            sortable: "custom",
            width:200,
            showTooltip: true,
          },
          {
            label: "启用状态",
            prop: "status",
            sortable: "custom",
            width: "110",
            formatter: (row) => {
              return row.status == "1" ? "停用" : "启用";
            },
          },
          {
            label: "值班地点",
            prop: "locationName",
            sortable: "custom",
          },
          {
            label: "值班人员类型",
            prop: "staffType",
            sortable: "custom",
            formatter: (row) => {
              return this.$store.getters.fineDirtDataName(
                "operation_staff_type",
                row.staffType
              );
            },
          },
          {
            label: "班别方案",
            prop: "classesName",
            sortable: "custom",
            width:200,
            showTooltip: true,
          },
          {
            label: "排班开始时间",
            prop: "startTime",
            sortable: "custom",
          },
          {
            label: "排班结束时间",
            prop: "endTime",
            sortable: "custom",
          },
          {
            label: "创建人",
            prop: "createByName",
            width: "100",
            sortable: "custom",
          },
          {
            label: "创建时间",
            prop: "createTime",
            sortable: "custom",
          },
        ],
      },
    };
  },
  created() {
    /* 添加监听事件 */
    window.addEventListener("storage", this.init);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.init);
  },
  mounted() {
    this.$refs.klTable.getTableConfig();
    this.init();
  },
  methods: {
    init() {
      this.$refs.klTable.searchData();
    },
    //获取列表数据
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      querySchedulPlanList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.list);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
      });
    },
    //新增
    add() {
      this.editFormData = {};
      this.show = true;
    },
    //提交上传表单
    async doSubmit() {
      try {
        let formData = await this.$refs.addDialog.getFormData();
        let res = await addOrUpdateSchedulPlan(formData);
        this.$utils.addOperateLog(res.data, "新建了排班方案");
        this.goDetailPage(res.data, "scheduleDetails", "edit");
        this.show = false;
        this.$refs.klTable.searchData();
      } catch (error) {
        console.log(error);
      }
    },
    //关闭上传表单
    doClose() {
      this.editFormData = {};
      this.filesList = [];
      this.show = false;
    },
    //跳转到详情页
    goDetailPage(id, router, type) {
      const { href } = this.$router.resolve({
        name: router,
        query: {
          id: id ? id : "",
          pageType: type ? type : "",
        },
      });
      window.open(href, "_blank");
    },
  },
};
</script>
<style lang="less" scoped>
.constentmain {
  width: 100%;
  height: 100%;
}
</style>